﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using NHibernate.Criterion;

namespace ZLCMS.Web.SystemManage.weixin.Response
{
    public partial class KeywrodRuleDetail : System.Web.UI.Page
    {
        public string MaterialTitle { get; set; }

        protected string _userAccount;
        protected FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            _framePage = new FrameHelper(this);

            if (!IsPostBack)
            {
                ParamKit.Bind("YesNo_10", ddl_match, null, null, null, "1", "1");     

                int uid = 0;
                WxResponseRule rule = null;
                if (Int32.TryParse(Request["uid"], out uid))
                {
                    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        rule = session.CreateCriteria<WxResponseRule>().Add(Expression.Eq("Id", uid)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxResponseRule>();
                        
                        WxMaterial mat = session.CreateCriteria<WxMaterial>().Add(Expression.Eq("Id", rule.ResponseMaterialId)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxMaterial>();
                       
                        if (mat != null)
                        {
                            MaterialTitle = mat.Title;
                        }
                    }
                    if (rule != null)
                    {
                        keywords.Text = rule.RequestKeyword;
                        title.Text = rule.RuleTitle;
                        SetMaterialControl1.MaterialId = rule.ResponseMaterialId;
                        ddl_match.SelectedValue = rule.FullMatch;
                        ViewState["uid"] = uid;
                    }
                }
            }
            else
            {
                WxResponseRule rule = null;
                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (uid > 0)
                    {
                        rule =session.CreateCriteria<WxResponseRule>().Add(Expression.Eq("Id", uid)).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxResponseRule>();
                    }
                    if (rule == null)
                    {
                        rule = new WxResponseRule();
                        rule.RequestType = "Keyword";
                        rule.Kind = "KEYWORD";
                        rule.IsAvailable = "1";
                        rule.OwnerAccount = _userAccount;
                    }
                    rule.RequestKeyword = keywords.Text;
                    rule.RuleTitle = title.Text;
                    rule.ResponseMaterialId = SetMaterialControl1.MaterialId;
                    rule.FullMatch = ddl_match.SelectedValue;

                    session.SaveOrUpdate(rule);
                    session.Flush();
                }

                _framePage = new FrameHelper(this);
                ReturnCode rc = new ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/weixin/Response/KeywrodRuleDetail.aspx?tabid=KeywrodRuleDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + rule.Id;
                }

                _framePage.Alert(rc);
            }
        }
    }
}